#include <stdio.h>
#include "ofc/DDoubleArray.h"

int main(int argc, char *argv[])
{
  DDoubleArray *dar1  = [DDoubleArray alloc];
  DDoubleArray *dar2  = [DDoubleArray new  ];
  DText         *str;

  double doubles1[] = { 10.0, 20.0, 30.0 };
  double doubles2[] = { 50.0, 30.0, 10.0 };

                                       // Init with an array of doubles
  [dar1 init :doubles1 :(sizeof(doubles1) / sizeof(double))];

  [dar2 size :50];                     // Resize the array

  printf("Length of array1:%ld.\n", [dar1 length]);

  str = [dar1 toText];
  printf("Array as string:%s.\n", [str cstring]);
  [str free];

                                       // Insert and delete values
  [dar2 push :40.0];
  [dar2 push :50.0];
  [dar2 push :60.0];

  [dar2 insert :1 :70];
  [dar2 delete :2];

  str = [dar2 toText];
  printf("Array (after Insert,delete) as string:%s.\n", [str cstring]);
  [str free];
                                       // Insert and delete ranges of values
  [dar2 insert :2 :1 :doubles1 :(sizeof(doubles1) / sizeof(double))];
  [dar2 delete :3 :4];
  
  str = [dar2 toText];
  printf("Array (after range insert and delete) as string:%s.\n", [str cstring]);
  [str free];
                                       // Append and prepend
  [dar2 append  :doubles2 :(sizeof(doubles2)/sizeof(double))];
  [dar2 prepend :doubles1 :(sizeof(doubles1)/sizeof(double))];
  
  str = [dar2 toText];
  printf("Array (after append and prepend) as string:%s.\n", [str cstring]);
  [str free];
                                       // Sort the array
  [dar2 sort :0 :-1];

  str = [dar2 toText];
  printf("Array (after sort) as string:%s.\n", [str cstring]);
  [str free];
                                       // Count a double value
  printf("Value 70.0 is %ld times present in the array.\n", [dar2 count :70.0 :0 :-1]);
                                       // Statistical methods
  printf("Sum of the values in the array:%.1f.\n", [dar2 sum :0 :-1]);
  printf("Max of the values in the array:%.1f.\n", [dar2 max :0 :-1]);
  printf("Min of the values in the array:%.1f.\n", [dar2 min :0 :-1]);
  printf("Avg of the values in the array:%.1f.\n", [dar2 average :0 :-1]);
  printf("StD of the values in the arary:%.1f.\n", [dar2 standardDeviation :0 :-1]);

                                       // Compare the arrays
  if ([dar1 compare :dar2] == 0)
    printf("Array1 is equal to array2.\n");
  else
    if ([dar1 compare :dar2] < 0)
      printf("Array1 is smaller than array2.\n");
    else
      printf("Array1 is greater than array2.\n");

                                       // Use as fifo
  [dar2 clear];

  [dar2 enqueue :3.0];
  [dar2 enqueue :4.0];

  printf("Fifo dequeue:%.1f.\n", [dar2 dequeue]);

                                       // Use as lifo
  [dar2 clear];

  [dar2 push :5.0];
  [dar2 push :7.0];

  printf("Lifo pop:%.1f.\n", [dar2 pop]);
  
  [dar1 free];                         // Cleanup
  [dar2 free];

  return 0;
}

  
